<?php

/**
 * FLARE_Controller
 * 
 * used to help setup flare cms specific things, all controllers MUST extend this rather then CI_Controller
 * in order to plugin into FlareCMS - Calls to parent::__construct(); in your controllers are a must as usual.
 */
class FLARE_Controller extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		
		// set the cms theme and language
		$this->db->select('setting_key, setting_value')
				 ->from('settings')
				 ->where_in('setting_key', array('theme', 'language', 'sitename'));
		$query = $this->db->get();
		if($query->num_rows() == 3)
		{
			foreach($query->result() as $row)
			{
				if($row->setting_key == 'sitename')
				{
					$key = 'sitename';
				}
				elseif($row->setting_key == 'theme')
				{
					$key = 'template_directory';
					$row->setting_value = APPPATH.'views/'.$row->setting_value.'/templates/';
				}
				else
				{
					$key = 'language';
				}
					
				$this->config->set_item($key, $row->setting_value);
			}	
		} else
		{
			// Failed to load settings - ERROORRRR
			show_error("There was an error initiating FlareCMS. Please make sure your language and theme is set in the database.", 404);		
		}		

		// load language
		$this->lang->load_lang('english');

		// do admin check
		// and setup if we are in admin
		if($this->router->fetch_directory() == 'admin/')
		{
			define("IN_ADMIN", TRUE);
			$this->config->set_item('template_directory', APPPATH.'views/admin/templates/');
			$this->config->set_item('compile_directory', APPPATH.'cache/smarty/compiled/admin');
			
			
			// perform admin checks
		}		
		
		// assign theme url
		$themeurl = str_replace('/templates/', '', $this->config->item('template_directory')); 
		$this->smarty->assign('themeurl', $themeurl);
		
		// set the smarty templates
        $this->smarty->template_dir      = $this->config->item('template_directory');
        $this->smarty->compile_dir       = $this->config->item('compile_directory');
        $this->smarty->cache_dir         = $this->config->item('cache_directory');
        $this->smarty->config_dir        = $this->config->item('config_directory');
        $this->smarty->exception_handler = null;		
           
        // assign sitename
        $this->smarty->assign('sitename', $this->config->item('sitename'));       
	}
}